
// <copyright file="DuplicatesFilteredEnumerable.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The duplicates filtered enumerable.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Generic
{
    using System.Collections;
    using System.Collections.Generic;

    using SubhadraSolutions.Sharp.Utils.Validation;

    /// <summary>
    /// The duplicates filtered enumerable.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public sealed class DuplicatesFilteredEnumerable<T> : IEnumerable<T>
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private readonly IEnumerable<T> adaptedObject;

        /// <summary>
        /// The comparer.
        /// </summary>
        private readonly IEqualityComparer<T> _comparer;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DuplicatesFilteredEnumerable{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        public DuplicatesFilteredEnumerable(IEnumerable<T> adaptedObject)
        {
            Guard.ArgumentNotNull(adaptedObject, "adaptedObject");
            this.adaptedObject = adaptedObject;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DuplicatesFilteredEnumerable{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        /// <param name="comparer">
        /// The comparer.
        /// </param>
        public DuplicatesFilteredEnumerable(IEnumerable<T> adaptedObject, IEqualityComparer<T> comparer)
            : this(adaptedObject)
        {
            Guard.ArgumentNotNull(comparer, "comparer");
            this._comparer = comparer;
        }

        #endregion Constructors and Finalizers

        #region Public Methods and Operators

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        public IEnumerator<T> GetEnumerator()
        {
            if (this._comparer == null)
            {
                return new DuplicatesFilteredEnumerator<T>(this.adaptedObject.GetEnumerator());
            }

            return new DuplicatesFilteredEnumerator<T>(this.adaptedObject.GetEnumerator(), this._comparer);
        }

        #endregion Public Methods and Operators

        #region Explicit Interface Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion Explicit Interface Methods
    }
}